<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of wandoujia
 *
 * @author sunday add 2014.5.28  modify by dragon.hu @2016.2.18
 */
require_once 'BaseChannel.php';

//豌豆荚
class Wandoujia implements BaseChannel {
	
function __construct() {
        //echo "construct";
    }

    //put your code here
    function android($data) {
    	$UID = "";
        $paymentKey = "";        
        $ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
    }

    function ios($data) {
    	$UID = "";
        $paymentKey = "";        
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
    }

	function paymentCallBack($paymentKey, $platform)
	{	
		require_once('include/wandoujia/rsa.php');	
		//echo "login";
		$orderLog = new ChargeOrder();
		$orderLog->log("wandoujia", $_POST);
		//print_r($_REQUEST);
		$content=$_REQUEST['content'];
		$content = stripslashes($content);
		$content = stripslashes($content);
		//echo " before content = $content ";
		//$content = urlencode($content);
		//echo " after content = $content ";
		
		$sign=$_REQUEST['sign'];
		$rsa=new Rsa;
		//echo " content = $content sign = $sign ";
		$result=$rsa->verify($content,$sign);
		//echo " result = $result ";
		if($result)
		{ 
			//验证通过
			//收到的content是json数据，在此需要转成数组
			$content = json_decode($content,true);
			//print_r($content);
			$orderId = $content['out_trade_no'];
			$amount = $content['money']/100;	//传过来的是分
			
			//进行发货
			if(false == $orderLog->recordOrder(STATUS_SUCCESS,  $amount, $orderId, "wandoujia", $platform,$content['orderId']))
			{
				$ret="fail";
				$orderLog->log("wandoujia",array("order"=>$orderId,"money"=>$amount,"result"=>$ret,"serverresult"=>"faild"));
				//echo " insert order:$ext error ";
			}else 
			{
				//支付成功
				$ret="success";
				$orderLog->log("wandoujia",array("order"=>$orderId,"money"=>$amount,"result"=>$ret,"serverresult"=>"success"));
			}
			 
		} else {
			//"$sig验证失败"
			//echo "   sign error ";
			$orderLog->log("wandoujia","  order:$orderId  sign error ");
			$ret="fail";
		}
		return $ret;
	}
}